package com.jsm.tf.net.command;

import com.jsm.tf.entity.User;
import com.jsm.tf.net.message.request.JoinRoomMessage;
import com.jsm.tf.net.message.response.JoinRoomResponseMessage;
import com.jsm.tf.net.session.ISessionManager;
import com.jsm.tf.room.RoomDTO;
import com.jsm.tf.service.RoomService;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class JoinRoomCommand implements Command<JoinRoomMessage> {
    @Resource
    private ISessionManager sessionManager;
    @Resource
    private RoomService roomService;

    @Override
    public void execute(JoinRoomMessage message, Channel channel) {
        User user = sessionManager.getUserByChannel(channel);
        long roomId = message.getRoomId();
        RoomDTO room = roomService.getById(roomId);
        sessionManager.enterRoom(roomId, user);
        JoinRoomResponseMessage joinRoomResponseMessage = new JoinRoomResponseMessage();
        joinRoomResponseMessage.setRoom(room);
        joinRoomResponseMessage.setSeq(message.getSeq());
        joinRoomResponseMessage.setSeq(message.getSeq());
        channel.writeAndFlush(joinRoomResponseMessage);
    }
}
